﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using StockTrader.DAL.HoseAdapter.Lib;
using System.Globalization;
using System.Collections;
using StockTrader.DAL.HoseAdapter.FileParser;
using StockTrader.DAL.HoseAdapter.FileParser.Structure;

namespace StockTrader.DAL.HoseAdapter.BusinessLogic
{
    public enum MarketState
    {
        BeginOfDay,
        Session1,
        Session2,
        Session3,
        Neogtiate,
        EndOfDay,
    }

    public class TransSchedule
    {
        private static string srcFile = ConfigurationManager.AppSettings["ROOT_PRS"] + "DATAPATH.MAP";
        private static string desFile = "datapath.map";

        public static MarketState getMarketState()
        {
            try
            {
                if (File.Exists(srcFile))
                    CommonLib.SafeCopy(srcFile, desFile);
                using (FileStream fs = new FileStream(desFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                {
                    using (StreamReader sr = new StreamReader(fs))
                    {
                        string temp = sr.ReadLine();
                        DateTime transDate = getTransDate();
                        if (!transDate.Equals(DateTime.Today.Date))
                            return MarketState.EndOfDay;
                        ArrayList list = FileEngineer.RetrieveFileInfo(transDate, typeof(MarketStat));
                        if ((ConvertMKT(list).IndexOf("K") > -1))
                            return MarketState.EndOfDay;
                        else if (ConvertMKT(list).IndexOf("C") > -1)
                            return MarketState.Neogtiate;
                        else if (ConvertMKT(list).IndexOf("A") > -1)
                            return MarketState.Session3;
                        else if (ConvertMKT(list).IndexOf("O") > -1)
                            return MarketState.Session2;
                        else if (ConvertMKT(list).IndexOf("P") > -1)
                            return MarketState.Session1;
                        else
                            return MarketState.BeginOfDay;
                    }
                }
            }
            catch
            {
                return MarketState.EndOfDay;
            }
        }

        private static List<string> ConvertMKT(ArrayList list)
        {
            List<string> state = new List<string>();
            try
            {
                foreach (MarketStat mk in list)
                    state.Add(mk.ControlCode);
                return state;
            }
            catch
            {
                return state;
            }
        }

        public static string getCurrentPRSdataFolder(string datapath)
        {
            try
            {
                return ConfigurationManager.AppSettings["ROOT_PRS"] + datapath.Substring(datapath.Length - 10, 10);
            }
            catch
            {
                throw;
            }
        }

        public static DateTime getTransDate()
        {
            try
            {
//                 if (File.Exists(desFile))
//                     File.Delete(desFile);
                CommonLib.SafeCopy(srcFile, desFile);
                using (FileStream fs = new FileStream(desFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                {
                    using (StreamReader sr = new StreamReader(fs))
                    {
                        string temp = sr.ReadLine();
                        CultureInfo rusiaCI = new CultureInfo("ru-RU", true);
                        return Convert.ToDateTime(temp.Substring(temp.Length - 18, 10), rusiaCI);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static DateTime getPreviousDate()
        {
            try
            {
                string srcFile = ConfigurationManager.AppSettings["ROOT_PRS"] + "datapath.map";
                string desFile = "datapath.map";

                if (!File.Exists(desFile))
                    CommonLib.SafeCopy(srcFile, desFile);
                using (FileStream fs = new FileStream(desFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                {
                    using (StreamReader sr = new StreamReader(fs))
                    {
                        string temp = sr.ReadLine();
                        CultureInfo rusiaCI = new CultureInfo("ru-RU", true);
                        return Convert.ToDateTime(temp.Substring(temp.Length - 36, 10), rusiaCI);
                    }
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        public static bool IsTradingDate()
        {
            return DateTime.Equals(DateTime.Now.Date, getTransDate().Date);
        }

        public static List<Froom> getLastRoom(string fileName)
        {
            FileStream fs = null;
            BinaryReader r = null;
            List<Froom> lastRoom = null;
            try
            {

                FileInfo fiSec = new FileInfo(fileName);
                using (fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
                {
                    lastRoom.Clear();
                    int intNumOfRecord = (int)(fiSec.Length / 20);
                    using (r = new BinaryReader(fs))
                    {
                        for (int i = 1; i <= intNumOfRecord; i++)
                        {
                            Froom fInfo = new Froom();
                            fInfo.StockNo = (int)r.ReadInt32();
                            fInfo.TotalRoom = r.ReadDouble();
                            fInfo.CurrentRoom = r.ReadDouble();
                            lastRoom.Add(fInfo);
                        }
                    }
                }
                return lastRoom;
            }
            catch (Exception ex)
            {
                throw;
            }
            finally
            {
                fs.Close();
                r.Close();
            }
        }
    }
}
